#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

#include	"config_parser.h"

//------------------------------------------------------------------------------
void close_sockets(config_info * sample) {
	int i;
	for (i = 0; i < sample->num_nodes; i++)
		close(sample->l.sid[i]);
}
//------------------------------------------------------------------------------
void create_tcp_server_socket_for_star(char * ip, int port, int expected_num_connections, int sids[MAX_RING_NODES]) {
	printf("HERE\n");
	unsigned int client_addr_len;
	int sid, e = -1, c_counter = 0, activate = 1;
	struct sockaddr_in server_addr, client_addr;

	client_addr_len = sizeof(client_addr);

	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(server_addr));
	assert(e >= 0);

	e = listen(sid, expected_num_connections);
	assert(e >= 0);

	printf("\nLeader waiting for connection requests from processes on the ring...%d\n", expected_num_connections);
	while (c_counter < expected_num_connections) {

		sids[c_counter] = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
		setsockopt(sids[c_counter], SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

		while (sids[c_counter] < 0)
			sids[c_counter] = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
		c_counter++;
		printf("Number of requests received: %d\n", c_counter);
	}
}
//------------------------------------------------------------------------------
int create_tcp_client_socket(const char *ip, int port) {
	struct sockaddr_in server_addr;
	int activate = 1;
	int e = -1;
	int sid;
	memset(&server_addr, '\0', sizeof(struct sockaddr_in));

	sid = socket(AF_INET, SOCK_STREAM, 0);

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	while (e < 0) {
		e = connect(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
		if (e >= 0) {
			//			setsock_nonblock(sid);
			break;
		}
		close(sid);
		sleep(1);
		sid = socket(AF_INET, SOCK_STREAM, 0);
	}
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	return sid;
}
//------------------------------------------------------------------------------
int create_tcp_server_socket(const char *ip, int port) {

	struct sockaddr_in server_addr, client_addr;
	int sid, e, activate = 1;
	unsigned int client_addr_len;

	client_addr_len = sizeof(client_addr);
	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid, 1);

	printf("Server: PID: %d Waiting for connection requests from predecessor...\n", get_config_info()->self_id);
	int new_sid = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
	setsockopt(new_sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	printf("connection request came from the node  on socket: %d\n", new_sid);

	//	setsock_nonblock(new_sid);

	return new_sid;
}
